package com.jiang.books.domain;

import java.io.Serializable;
import java.math.BigInteger;
import java.util.HashSet;
import java.util.Set;

import javax.persistence.*;
import javax.validation.constraints.Size;

import static javax.persistence.GenerationType.SEQUENCE;

/*
    Create table jiang_country
    (
		id             Number(20,0) NOT NULL ENABLE,
		country_name   Varchar2(200),
        CONSTRAINT pk_countries PRIMARY KEY (id),
		CONSTRAINT country_name_unique UNIQUE (country_name)
    );
 */

@Entity
@Table(name = "jiang_country")
@NamedQueries({
	@NamedQuery(name="Country.findAll",
			    query="select c from Country c"),
    @NamedQuery(name="Country.findById",
			    query="select distinct c from Country c where c.id = :id")
})
@SqlResultSetMapping(
		name="countryResult",
		entities=@EntityResult(entityClass=Country.class)
)
public class Country implements Serializable {

	private static final long serialVersionUID = 1L;
	
	private BigInteger id;
    private String countryName;

	private Set<Author> authors = new HashSet<Author>();

	public Country() {
	}

	public Country(String countryName) {
		this.countryName = countryName;
	}

	public Country(String countryName, Set<Author> authors) {
        this.countryName = countryName;
		this.authors = authors;
	}

    @Id
    @GeneratedValue(strategy = SEQUENCE, generator="JIANG_SEQ")
    @SequenceGenerator(name="JIANG_SEQ", sequenceName="jiang_id_sequence", allocationSize=100)
    @Column(name = "ID", unique = true, nullable = false)
    public BigInteger getId() {
        return this.id;
    }

    public void setId(BigInteger id) {
        this.id = id;
    }

    @Column(name = "COUNTRY_NAME")
    @Size(min=2, max=60)
    public String getCountryName() {
        return this.countryName;
    }

    public void setCountryName(String countryName) {
        this.countryName = countryName;
    }


	@OneToMany(mappedBy = "country", cascade=CascadeType.ALL, orphanRemoval=true)
    public Set<Author> getAuthors() {
        return this.authors;
    }

    public void setAuthors(Set<Author> authors) {
        this.authors = authors;
    }
    
    
	@Transient
	public boolean isNew() {
		if (id == null) {
			return true;
		} else {
			return false;
		}
	}

	public String toString() {
		return "Country :" + getId() + ", " + getCountryName();
	}
}